
$(function() {
	
	$("#registrationForm").bootstrapValidator();
	$("#registrationForm").on('success.form.bv', function(e) {
        e.preventDefault();
    });
	
	var from = $('#from').val();
	
	if(from == "check"){
		$('#registrationForm').find('input').attr('disabled', 'disabled');
		$('#registrationForm').find('select').attr('disabled', 'disabled');
		$('#registrationForm').find('textarea').attr('disabled', 'disabled');
		$('#registrationForm').find('button').attr('disabled', 'disabled');
	}
	
	//异步加载体检库
	$.ajax({
		url : ctx + '/medical/getAllMedical',
		dataType : 'json',
		type : 'post',
		success : function(data){
			
			$('#medical').html('<option value="0">请选择</option>');
			
			var selected = $('#medical').data('selected');
			
			console.log(selected);
			
			for(var i=0; i<data.list.length; i++){
				if(data.list[i].id == selected){
					$('#medical').append('<option selected="selected" value="'+data.list[i].id+'">'+data.list[i].name+'</option>');
					$('#medical').change();
				}else{
					$('#medical').append('<option value="'+data.list[i].id+'">'+data.list[i].name+'</option>');
				}
				
			}
		},
		error : function(){
			alert("系统错误");
		}
	});
	
	//联动加载体检项目
	$('#medical').change(function(){
		
		var medicalId = $(this).val();
		
		$.ajax({
			url : ctx + '/subjects/getAllSubject',
			data : {medicalId : medicalId},
			dataType : 'json',
			type : 'post',
			success : function(data){
				$('#subjects').html('');
				
				var selected = $('#subjects').data('selected');
				
				if(data.list != null){
					for(var i=0; i<data.list.length; i++){
						if(data.list[i].id == selected){
							$('#subjects').append('<option selected="selected" value="'+data.list[i].id+'">'+data.list[i].name+'</option>');
						}else{
							$('#subjects').append('<option value="'+data.list[i].id+'">'+data.list[i].name+'</option>');
						}
						
					}
				}
				
				if(data.list == null || data.list.length <= 0)
					$('#subjects').html('<option value="0">请选择</option>');
				
			},
			error : function(){
				alert("系统错误");
			}
		});
		
	});
	
	//异步加载科室
	$.ajax({
		url : ctx + '/department/getAllDepartment',
		dataType : 'json',
		type : 'post',
		success : function(data){
			
			$('#department').html('<option value="0">请选择</option>');
			
			for(var i=0; i<data.list.length; i++){
				$('#department').append('<option value="'+data.list[i].id+'">'+data.list[i].name+'</option>');
			}
		},
		error : function(){
			alert("系统错误");
		}
	});
	
	$('#cancelButton').click(function(){
		
		$('#departmentName').val('');
		$('#departmentId').val('');
		
		$('#deparment').modal('hide');
	});
	
	$('#confirmButton').click(function(){
		$('#deparment').modal('hide');
	});

});


function validateForm(form, callback, confirmMsg){
	
	
	 var $form = $(form);

     var formData = $form.data('bootstrapValidator');
     if (formData) {
    	 // 修复记忆的组件不验证
    	 formData.validate();

		if (!formData.isValid()) {
			return false;
		}
    }
	
	$.ajax({
		url : ctx + '/medicalContent/addMedicalContent',
		data : $('#registrationForm').serialize(),
		dataType : 'json',
		type : 'post',
		success : function(data){
			if(data.status != "success"){
				alert(data.msg);
			}else{
				location.href = ctx + '/medicalContent/medicalContent';
			}
		},
		error : function(){
			alert("系统错误");
		}
	});
	
	return false;
}

//科室表单查询
function submitForm(){
	//加载科室树
	$.ajax({
		url : ctx + '/medical/getAllMedicalAndDepartment',
		data : $('#searchForm').serialize(),
		dataType : 'json',
		type : 'post',
		success : function(data){
			
			var zNodes =[];
			
			if(data.list != null){
				
				for(var i=0; i<data.list.length; i++){
					
					var subject = {};
					subject['id'] = data.list[i].id;
					subject['pId'] = 0;
					subject['name'] = data.list[i].institutionsName;
					subject['oid'] = data.list[i].id;
					
					zNodes.push(subject);
					
					if(data.list[i].depart != null){
						
						for(var j=0; j<data.list[i].depart.length; j++){
							var content = {};
							
							var id = data.list[i].depart[j].id;
							var name = data.list[i].depart[j].name;
							
							content['id'] = data.list[i].id + '' + id;
							content['pId'] = data.list[i].id;
							content['name'] = name;
							content['oid'] = id;
							content['click'] = "selectDepartment("+id+", '"+name+"')";
							zNodes.push(content);
						}
					}
				}
			}
			
			var setting = {
				check: {
					enable: false
				},
				data: {
					simpleData: {
						enable: true
					}
				}
			};
			
			//树加载
			$.fn.zTree.init($("#departTree"), setting, zNodes);
			var treeObj = $.fn.zTree.getZTreeObj("departTree");
			treeObj.expandAll(true);
			
		},
		error : function(){
			toastr.error('系统错误');
		}
	});
	
	return false;
	
}


//科室加载
function selectDepart(){
	
	//加载科室树
	$.ajax({
		url : ctx + '/medical/getAllMedicalAndDepartment',
		dataType : 'json',
		type : 'post',
		success : function(data){
			
			var zNodes =[];
			
			if(data.list != null){
				
				for(var i=0; i<data.list.length; i++){
					
					var subject = {};
					subject['id'] = data.list[i].id;
					subject['pId'] = 0;
					subject['name'] = data.list[i].institutionsName;
					subject['oid'] = data.list[i].id;
					
					zNodes.push(subject);
					
					if(data.list[i].depart != null){
						
						for(var j=0; j<data.list[i].depart.length; j++){
							var content = {};
							
							var id = data.list[i].depart[j].id;
							var name = data.list[i].depart[j].name;
							
							content['id'] = data.list[i].id + '' + id;
							content['pId'] = data.list[i].id;
							content['name'] = name;
							content['oid'] = id;
							content['click'] = "selectDepartment("+id+", '"+name+"')";
							zNodes.push(content);
						}
					}
				}
			}
			
			var setting = {
				check: {
					enable: false
				},
				data: {
					simpleData: {
						enable: true
					}
				}
			};
			
			//树加载
			$.fn.zTree.init($("#departTree"), setting, zNodes);
			var treeObj = $.fn.zTree.getZTreeObj("departTree");
			treeObj.expandAll(true);
			
/*			for(var i=0; i<zznode.length; i++){
				
				var checkedNode = treeObj.getNodeByParam("id", zznode[i].id, null);
				
				if(zznode[i].pId != null)
					treeObj.checkNode(checkedNode, true, true);
			}*/
			
		},
		error : function(){
			toastr.error('系统错误');
		}
	});
	
	$('#keyWord').val('');
	$('#deparment').modal('show');
	
}



function selectDepartment(id, name){
	
	$('#departmentName').val(name);
	$('#departmentId').val(id);
	
	$('#deparment').modal('hide');
}














